package com.zhc.dejingapi.service.upload.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhc.dejingapi.entity.BommakeEntity;
import com.zhc.dejingapi.entity.ProductClassEntity;
import com.zhc.dejingapi.entity.ProductInfoEntity;
import com.zhc.dejingapi.entity.ProductUnitEntity;
import com.zhc.dejingapi.mapper.*;
import com.zhc.dejingapi.pojo.qingflow.QingflowApiInfo;
import com.zhc.dejingapi.pojo.qingflow.appdata.adddata.QingAddDataResultMain;
import com.zhc.dejingapi.pojo.qingflow.queryresult.QingQueryActionResultBase;
import com.zhc.dejingapi.service.upload.IGaogeErpBaseInfoUploadService;
import com.zhc.dejingapi.utils.qing.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class GaogeErpBaseInfoUploadServiceImpl implements IGaogeErpBaseInfoUploadService {


    //各业务类型的mappger
    @Autowired
    BommakeMapper bommakeMapper;

    @Autowired
    BomworkMapper bomworkMapper;

    @Autowired
    ProductUnitMapper productUnitMapper;

    @Autowired
    ProductClassMapper productClassMapper;

    @Autowired
    ProductInfoMapper productInfoMapper;


    private final JdbcTemplate mysqlJdbcTemplate;
    private final JdbcTemplate qingflowdbJdbcTemplate;

    @Autowired
    QingflowApiInfo qqingflowApiInfo;

    private final RedisTemplate<String,Object> redisTemplate;


    //系统能用对象mapper

    //region 系统能用对象mapper
    @Autowired
    private QingAppfieldsMapper qingAppfieldsMapper;
    @Autowired
    private QingAppinfoMapper qingAppinfoMapper;

    @Autowired
    private QingAppfieldtablevaluesMapper qingAppfieldtablevaluesMapper;
    //endregion

    public GaogeErpBaseInfoUploadServiceImpl(@Qualifier("mySqlJdbcTemplate")JdbcTemplate mysqlJdbcTemplate1,
                                             @Qualifier("mySqlQFlowJdbcTemplate")JdbcTemplate qingflowdbJdbcTemplate1,
                                             @Qualifier("redisTemplateWin")RedisTemplate<String, Object> redisTemplate) {
        this.mysqlJdbcTemplate = mysqlJdbcTemplate1;
        this.qingflowdbJdbcTemplate = qingflowdbJdbcTemplate1;
        this.redisTemplate = redisTemplate;
    }



    @Override
    public boolean uploadBomMake() {

//qingflowApiInfo qqingflowApiInfo1,JdbcTemplate mysqlJdbcTemplate1,
//                              QingAppfieldsMapper qingAppfieldsMapper1,
//                              QingAppfieldtablevaluesMapper qingAppfieldTableValuesMapper1,
//                              BommakeMapper bommakeMapper1,
//                              String  strErpTableName
        BomMakeUploadUtils bomMakeUploadUtils =null;
        try{
            bomMakeUploadUtils = new BomMakeUploadUtils(this.qqingflowApiInfo,
                    this.mysqlJdbcTemplate,this.qingAppfieldsMapper,
                    this.qingAppfieldtablevaluesMapper,
                    this.bommakeMapper);
        }
        catch (Exception e){
            log.error(e.getMessage()+"\n"+e.getStackTrace());
            return  false;
        }

        if(bomMakeUploadUtils==null){
            return  false;
        }

        try {

            List<BommakeEntity> listNoUpdate =null  ;

            //region 新增初始行
            QueryWrapper<BommakeEntity> bommakeQueryWrapper = new QueryWrapper<>();
            bommakeQueryWrapper.last(" where (lastRequestId IS NULL or lastRequestId='') AND (isWaiting=0  or isWaiting is NULL ) AND (applyId IS NULL OR applyId=0)");

            listNoUpdate = bommakeMapper.selectList(bommakeQueryWrapper);
            if (listNoUpdate == null || listNoUpdate.isEmpty()) {

            } else {
                //Action  shuold be "A"
                for (int i = 0; i < listNoUpdate.size(); i++) {
                    BommakeEntity bommakeEntity = listNoUpdate.get(i);
                    try {
                        bomMakeUploadUtils.AddBomMake(bommakeEntity);
                    } catch (Exception e) {
                        bommakeEntity.setErrCode("-10076");
                        bommakeEntity.setErrMsg(e.getMessage() + "\n" + e.getStackTrace());
                        bommakeEntity.setLastRequestId(null);
                        bommakeEntity.setIsDeal(0);
                        bommakeEntity.setIsWaiting(0);
                        bommakeMapper.updateById(bommakeEntity);
                    }
                }
            }
            //endregion



            //region 新增行
            bommakeQueryWrapper = new QueryWrapper<>();
            bommakeQueryWrapper.last(" where  (lastRequestId is null or lastRequestId='') and updAction='A' AND (isWaiting=0  or isWaiting is NULL )  and applyId>0");


            listNoUpdate = null;
            listNoUpdate = bommakeMapper.selectList(bommakeQueryWrapper);
            if (listNoUpdate == null || listNoUpdate.isEmpty()) {

            } else {
                //Action  shuold be "A"
                for (int i = 0; i < listNoUpdate.size(); i++) {
                    BommakeEntity bommakeEntity = listNoUpdate.get(i);
                    try {
                        bomMakeUploadUtils.AddBomMake(bommakeEntity);
                    } catch (Exception e) {
                        bommakeEntity.setErrCode("-10076");
                        bommakeEntity.setErrMsg(e.getMessage() + "\n" + e.getStackTrace());
                        bommakeEntity.setLastRequestId(null);
                        bommakeEntity.setIsDeal(0);
                        bommakeEntity.setIsWaiting(0);
                        bommakeMapper.updateById(bommakeEntity);
                    }

                }
            }
            //endregion

            //修改

            //region 修改行
            bommakeQueryWrapper = new QueryWrapper<>();
            bommakeQueryWrapper.last(" where  (lastRequestId is null or lastRequestId='') and updAction='M' AND (isWaiting=0  or isWaiting is NULL )  and applyId>0");


            listNoUpdate = null;
            listNoUpdate = bommakeMapper.selectList(bommakeQueryWrapper);
            if (listNoUpdate == null || listNoUpdate.isEmpty()) {

            } else {
                //Action  shuold be "A"
                for (int i = 0; i < listNoUpdate.size(); i++) {
                    BommakeEntity bommakeEntity = listNoUpdate.get(i);
                    try {
                        bomMakeUploadUtils.UpdBomMake(bommakeEntity,"A");
                    } catch (Exception e) {
                        bommakeEntity.setErrCode("-10076");
                        bommakeEntity.setErrMsg(e.getMessage() + "\n" + e.getStackTrace());
                        bommakeEntity.setLastRequestId(null);
                        bommakeEntity.setIsDeal(0);
                        bommakeEntity.setIsWaiting(0);
                        bommakeMapper.updateById(bommakeEntity);
                    }

                }
            }
            //endregion

            //删除条件

            //region 删除行
            bommakeQueryWrapper = new QueryWrapper<>();
            bommakeQueryWrapper.last(" where  (lastRequestId is null or lastRequestId='') and updAction='D' AND (isWaiting=0  or isWaiting is NULL )  and applyId>0");


            listNoUpdate = null;
            listNoUpdate = bommakeMapper.selectList(bommakeQueryWrapper);
            if (listNoUpdate == null || listNoUpdate.isEmpty()) {

            } else {
                //Action  shuold be "D"
                for (int i = 0; i < listNoUpdate.size(); i++) {
                    BommakeEntity bommakeEntity = listNoUpdate.get(i);
                    //判断有没有使用，如果使用，只能修改，否则调删除
                    String strSql="SELECT COUNT(*)\n" +
                            " from bomwork\n" +
                            " WHERE MAKER=?";
                    int iHaveCount=mysqlJdbcTemplate.queryForObject(strSql,new Object[]{bommakeEntity.getCode()},Integer.class);
                    if(iHaveCount>0){
                        try {
                            bomMakeUploadUtils.UpdBomMake(bommakeEntity,"R");
                        } catch (Exception e) {
                            bommakeEntity.setErrCode("-10076");
                            bommakeEntity.setErrMsg(e.getMessage() + "\n" + e.getStackTrace());
                            bommakeEntity.setLastRequestId(null);
                            bommakeEntity.setIsDeal(0);
                            bommakeEntity.setIsWaiting(0);
                            bommakeMapper.updateById(bommakeEntity);
                        }

                    }
                    else{
                        try {
                            bomMakeUploadUtils.DelBomMake(bommakeEntity);
                        } catch (Exception e) {
                            bommakeEntity.setErrCode("-10076");
                            bommakeEntity.setErrMsg(e.getMessage() + "\n" + e.getStackTrace());
                            bommakeEntity.setLastRequestId(null);
                            bommakeEntity.setIsDeal(0);
                            bommakeEntity.setIsWaiting(0);
                            bommakeMapper.updateById(bommakeEntity);
                        }
                    }






                }
            }
            //endregion


        }
        catch (Exception e){
            log.error(e.getMessage()+"\n"+e.getStackTrace());
        }
        return true;
    }

    @Override
    public boolean QueryBomMakeAddActionResult() {

        QueryWrapper<BommakeEntity> queryWrapper=new QueryWrapper<>();

        queryWrapper=null;
        queryWrapper=new QueryWrapper<>();

        //queryWrapper.eq("isDeal",0)
        //        .and(wq->wq.eq("lastRequestId","")
        //                .or().isNull("lastRequestId")
        //        );
        queryWrapper.last(" where isDeal=0 and isWaiting=1 and NOT (lastRequestId is NULL or lastRequestId='')");

        List<BommakeEntity> listQuery=bommakeMapper.selectList(queryWrapper);
        if (listQuery==null||listQuery.isEmpty()){

        }
        else {
            for (int i = 0; i < listQuery.size(); i++) {
                if(listQuery.get(i).getLastRequestId()==null||listQuery.get(i).getLastRequestId().isEmpty()){

                }
                else {
                    BommakeEntity bommakeEntity=listQuery.get(i);
                    try{
                        QingQueryActionResultBase actionResultBase= QingflowAPIUtils.GetRequestResult(qqingflowApiInfo,listQuery.get(i).getLastRequestId());
                        if (actionResultBase==null){
                            bommakeEntity.setErrCode("-10066");
                            bommakeEntity.setErrMsg("返回出错：返回内容为空！");
                            bommakeMapper.updateById(bommakeEntity);
                        }
                        else{
                            if (actionResultBase.getErrorCode()==0){
                                if (bommakeEntity.getActionResult().equals("A")){
                                    bommakeEntity.setErrCode("0");
                                    bommakeEntity.setErrMsg("更新成功！");
                                    bommakeEntity.setLastRequestId(null);
                                    bommakeEntity.setIsWaiting(0);
                                    bommakeEntity.setIsNeedUpd(0);
                                    //bommakeEntity.setUpdAction("");//清除
                                    bommakeEntity.setIsDeal(1);
                                    try {
                                        long applyId= Long.parseLong(actionResultBase.getResult());
                                        bommakeEntity.setApplyId(applyId);
                                    }
                                    catch (Exception e){
                                        e.printStackTrace();
                                    }

                                }
                                else if (bommakeEntity.getActionResult().equals("D")){

                                    try {
                                         bommakeMapper.deleteById(bommakeEntity);
                                    }
                                    catch (Exception e){
                                        e.printStackTrace();
                                    }

                                }

                                bommakeMapper.updateById(bommakeEntity);
                            }
                            else{
                                bommakeEntity.setErrCode(actionResultBase.getErrorCode()+"");
                                bommakeEntity.setErrMsg(actionResultBase.getMessage());
                                bommakeEntity.setLastRequestId(null);
                                bommakeEntity.setIsWaiting(0);
                                bommakeMapper.updateById(bommakeEntity);
                            }
                        }
                    }
                    catch (Exception e){
                        bommakeEntity.setErrCode("-10076");
                        bommakeEntity.setErrMsg("返回出错："+e.getMessage()+"\n"+e.getStackTrace());
                        bommakeMapper.updateById(bommakeEntity);
                    }

                }
            }
        }

        return true;
    }

    @Override
    public boolean uploadProductUnit() {

        boolean bSuccess=false;
        Map<String,String> mapContrastFields=new HashMap<>();
        mapContrastFields.put("单位编码","unitCode");
        mapContrastFields.put("单位名称","unitName");
        mapContrastFields.put("标识码","eri");
        //mapContrastFields.put("状态","");
        mapContrastFields.put("备注","unitEName");

        QingDataUploadUtils UploadUtils =null;
        try{
            UploadUtils = new QingDataUploadUtils(this.qqingflowApiInfo,
                    this.mysqlJdbcTemplate,this.qingAppfieldsMapper,
                    this.qingAppfieldtablevaluesMapper,
                    mapContrastFields,
                    "product_unit");
        }
        catch (Exception e){
            log.error(e.getMessage()+"\n"+e.getStackTrace());
            return  false;
        }

        if(UploadUtils==null){
            return  false;
        }

        ProductUnitEntity entity =null  ;

        ProductUnitMapper mapper=productUnitMapper;

        try {

            List<ProductUnitEntity> listNoUpdate =null  ;

            //region 新增初始行
            QueryWrapper<ProductUnitEntity> addQueryWrapper = new QueryWrapper<>();
            addQueryWrapper.last(" where (lastRequestId IS NULL or lastRequestId='') AND (isWaiting=0  or isWaiting is NULL ) AND (applyId IS NULL OR applyId=0)");

            listNoUpdate = mapper.selectList(addQueryWrapper);
            if (listNoUpdate == null || listNoUpdate.isEmpty()) {

            } else {
                //Action  shuold be "A"
                for (int i = 0; i < listNoUpdate.size(); i++) {
                    entity = listNoUpdate.get(i);
                    try {
                        QingAddDataResultMain resultMain = UploadUtils.AddEntity(entity);
                        //region 处理返回结果
                        if (resultMain == null) {

                            entity.setErrCode("-10015");
                            entity.setErrMsg("服务器返回内容为空！");
                            entity.setLastRequestId(null);
                            entity.setIsDeal(0);
                            entity.setIsWaiting(0);
                            entity.setDealDate(LocalDateTime.now());
                            mapper.updateById(entity);

                        } else {
                            if (resultMain.getErrCode() == 0) {
                                entity.setErrCode(resultMain.getErrCode().toString());
                                entity.setErrMsg("提交成功！");
                                entity.setLastRequestId(resultMain.getResult().getRequestId());
                                entity.setIsDeal(0);
                                entity.setIsWaiting(1);
                                entity.setDealDate(LocalDateTime.now());
                                entity.setActionResult("A");
                                mapper.updateById(entity);

                            } else {
                                entity.setErrCode(resultMain.getErrCode().toString());
                                entity.setErrMsg(resultMain.getErrMsg());
                                entity.setLastRequestId(null);
                                entity.setIsDeal(0);
                                entity.setIsWaiting(0);
                                entity.setDealDate(LocalDateTime.now());
                                mapper.updateById(entity);
                            }

                        }
                        //endregion
                    } catch (Exception e) {
                        entity.setErrCode("-10076");
                        entity.setErrMsg(e.getMessage() + "\n" + e.getStackTrace());
                        entity.setLastRequestId(null);
                        entity.setIsDeal(0);
                        entity.setIsWaiting(0);
                        mapper.updateById(entity);
                    }
                }
            }
            //endregion



            //region 新增行
            addQueryWrapper = new QueryWrapper<>();
            addQueryWrapper.last(" where  (lastRequestId is null or lastRequestId='') and updAction='A' AND (isWaiting=0  or isWaiting is NULL )  and applyId>0");


            listNoUpdate = null;
            listNoUpdate = mapper.selectList(addQueryWrapper);
            if (listNoUpdate == null || listNoUpdate.isEmpty()) {

            } else {
                //Action  shuold be "A"
                for (int i = 0; i < listNoUpdate.size(); i++) {
                    entity = listNoUpdate.get(i);
                    try {
                        QingAddDataResultMain resultMain= UploadUtils.AddEntity(entity);
                    } catch (Exception e) {
                        entity.setErrCode("-10076");
                        entity.setErrMsg(e.getMessage() + "\n" + e.getStackTrace());
                        entity.setLastRequestId(null);
                        entity.setIsDeal(0);
                        entity.setIsWaiting(0);
                        mapper.updateById(entity);
                    }

                }
            }
            //endregion

            //修改

            //region 修改行
            addQueryWrapper = new QueryWrapper<>();
            addQueryWrapper.last(" where  (lastRequestId is null or lastRequestId='') and updAction='M' AND (isWaiting=0  or isWaiting is NULL )  and applyId>0");


            listNoUpdate = null;
            listNoUpdate = mapper.selectList(addQueryWrapper);
            if (listNoUpdate == null || listNoUpdate.isEmpty()) {

            } else {
                //Action  shuold be "A"
                for (int i = 0; i < listNoUpdate.size(); i++) {
                    entity = listNoUpdate.get(i);
                    try {
                        QingAddDataResultMain resultMain=  UploadUtils.UpdEntity(entity,"A");
                        //region 处理返回结果
                        if (resultMain == null) {

                            entity.setErrCode("-10015");
                            entity.setErrMsg("服务器返回内容为空！");
                            entity.setLastRequestId(null);
                            entity.setIsDeal(0);
                            entity.setIsWaiting(0);
                            entity.setDealDate(LocalDateTime.now());
                            mapper.updateById(entity);

                        } else {
                            if (resultMain.getErrCode() == 0) {
                                entity.setErrCode(resultMain.getErrCode().toString());
                                entity.setErrMsg("提交成功！");
                                entity.setLastRequestId(resultMain.getResult().getRequestId());
                                entity.setIsDeal(0);
                                entity.setIsWaiting(1);
                                entity.setDealDate(LocalDateTime.now());
                                entity.setActionResult("A");
                                mapper.updateById(entity);

                            } else {
                                entity.setErrCode(resultMain.getErrCode().toString());
                                entity.setErrMsg(resultMain.getErrMsg());
                                entity.setLastRequestId(null);
                                entity.setIsDeal(0);
                                entity.setIsWaiting(0);
                                entity.setDealDate(LocalDateTime.now());
                                mapper.updateById(entity);
                            }

                        }
                        //endregion
                    } catch (Exception e) {
                        entity.setErrCode("-10076");
                        entity.setErrMsg(e.getMessage() + "\n" + e.getStackTrace());
                        entity.setLastRequestId(null);
                        entity.setIsDeal(0);
                        entity.setIsWaiting(0);
                        mapper.updateById(entity);
                    }

                }
            }
            //endregion

            //删除条件

            //region 删除行
            addQueryWrapper = new QueryWrapper<>();
            addQueryWrapper.last(" where  (lastRequestId is null or lastRequestId='') and updAction='D' AND (isWaiting=0  or isWaiting is NULL )  and applyId>0");


            listNoUpdate = null;
            listNoUpdate = mapper.selectList(addQueryWrapper);
            if (listNoUpdate == null || listNoUpdate.isEmpty()) {

            } else {
                //Action  shuold be "D"
                for (int i = 0; i < listNoUpdate.size(); i++) {
                    entity = listNoUpdate.get(i);
                    //判断有没有使用，如果使用，只能修改，否则调删除
                    String strSql="SELECT count(*) FROM product_info where unitCode=?";
                    int iHaveCount=mysqlJdbcTemplate.queryForObject(strSql,new Object[]{entity.getUnitCode()},Integer.class);


                    if(iHaveCount>0){
                        try {
                            QingAddDataResultMain resultMain= UploadUtils.UpdEntity(entity,"R");
                            //region 处理返回结果
                            if (resultMain == null) {

                                entity.setErrCode("-10015");
                                entity.setErrMsg("服务器返回内容为空！");
                                entity.setLastRequestId(null);
                                entity.setIsDeal(0);
                                entity.setIsWaiting(0);
                                entity.setDealDate(LocalDateTime.now());
                                mapper.updateById(entity);

                            } else {
                                if (resultMain.getErrCode() == 0) {
                                    entity.setErrCode(resultMain.getErrCode().toString());
                                    entity.setErrMsg("提交成功！");
                                    entity.setLastRequestId(resultMain.getResult().getRequestId());
                                    entity.setIsDeal(0);
                                    entity.setIsWaiting(1);
                                    entity.setDealDate(LocalDateTime.now());
                                    entity.setActionResult("A");
                                    mapper.updateById(entity);

                                } else {
                                    entity.setErrCode(resultMain.getErrCode().toString());
                                    entity.setErrMsg(resultMain.getErrMsg());
                                    entity.setLastRequestId(null);
                                    entity.setIsDeal(0);
                                    entity.setIsWaiting(0);
                                    entity.setDealDate(LocalDateTime.now());
                                    mapper.updateById(entity);
                                }

                            }
                            //endregion
                        } catch (Exception e) {
                            entity.setErrCode("-10076");
                            entity.setErrMsg(e.getMessage() + "\n" + e.getStackTrace());
                            entity.setLastRequestId(null);
                            entity.setIsDeal(0);
                            entity.setIsWaiting(0);
                            mapper.updateById(entity);
                        }

                    }
                    else{
                        try {
                            QingAddDataResultMain resultMain = UploadUtils.DelEntity(entity);

                            //region 处理返回结果

                            if (resultMain == null) {

                                entity.setErrCode("-10015");
                                entity.setErrMsg("服务器返回内容为空！");
                                entity.setLastRequestId(null);
                                entity.setIsDeal(0);
                                entity.setIsWaiting(0);
                                entity.setDealDate(LocalDateTime.now());
                                mapper.updateById(entity);

                            } else {
                                if (resultMain.getErrCode() == 0) {
                                    entity.setErrCode(resultMain.getErrCode().toString());
                                    entity.setErrMsg("提交成功！");
                                    entity.setLastRequestId(resultMain.getResult().getRequestId());
                                    entity.setIsDeal(0);
                                    entity.setIsWaiting(1);
                                    entity.setDealDate(LocalDateTime.now());
                                    entity.setActionResult("D");
                                    mapper.updateById(entity);
                                    //增加马上判断的结果

                                } else {
                                    entity.setErrCode(resultMain.getErrCode().toString());
                                    entity.setErrMsg(resultMain.getErrMsg());
                                    entity.setLastRequestId(null);
                                    entity.setIsDeal(0);
                                    entity.setIsWaiting(0);
                                    entity.setDealDate(LocalDateTime.now());
                                    mapper.updateById(entity);
                                }

                            }
                            //endregion

                        } catch (Exception e) {
                            entity.setErrCode("-10076");
                            entity.setErrMsg(e.getMessage() + "\n" + e.getStackTrace());
                            entity.setLastRequestId(null);
                            entity.setIsDeal(0);
                            entity.setIsWaiting(0);
                            mapper.updateById(entity);
                        }
                    }

                }
            }
            //endregion


        }
        catch (Exception e){
            log.error(e.getMessage()+"\n"+e.getStackTrace());
        }



        return bSuccess;
    }

    @Override
    public boolean QueryProductUnitActionResult() {

        QueryWrapper<ProductUnitEntity> queryWrapper=new QueryWrapper<>();
        ProductUnitEntity entity =null  ;

        ProductUnitMapper mapper=productUnitMapper;
        queryWrapper=null;
        queryWrapper=new QueryWrapper<>();

        //queryWrapper.eq("isDeal",0)
        //        .and(wq->wq.eq("lastRequestId","")
        //                .or().isNull("lastRequestId")
        //        );
        queryWrapper.last(" where isDeal=0 and isWaiting=1 and NOT (lastRequestId is NULL or lastRequestId='')");

        List<ProductUnitEntity> listQuery=mapper.selectList(queryWrapper);
        if (listQuery==null||listQuery.isEmpty()){

        }
        else {
            for (int i = 0; i < listQuery.size(); i++) {
                if(listQuery.get(i).getLastRequestId()==null||listQuery.get(i).getLastRequestId().isEmpty()){

                }
                else {
                    entity=listQuery.get(i);
                    try{
                        QingQueryActionResultBase actionResultBase= QingflowAPIUtils.GetRequestResult(qqingflowApiInfo,listQuery.get(i).getLastRequestId());
                        if (actionResultBase==null){
                            entity.setErrCode("-10066");
                            entity.setErrMsg("返回出错：返回内容为空！");
                            mapper.updateById(entity);
                        }
                        else{
                            if (actionResultBase.getErrorCode()==0){
                                if (entity.getActionResult().equals("A")){
                                    entity.setErrCode("0");
                                    entity.setErrMsg("更新成功！");
                                    entity.setLastRequestId(null);
                                    entity.setIsWaiting(0);
                                    entity.setIsNeedUpd(0);
                                    //entity.setUpdAction("");//清除
                                    entity.setIsDeal(1);
                                    try {
                                        long applyId= Long.parseLong(actionResultBase.getResult());
                                        entity.setApplyId(applyId);
                                    }
                                    catch (Exception e){
                                        e.printStackTrace();
                                    }

                                }
                                else if (entity.getActionResult().equals("D")){

                                    try {
                                        mapper.deleteById(entity);
                                    }
                                    catch (Exception e){
                                        e.printStackTrace();
                                    }

                                }

                                mapper.updateById(entity);
                            }
                            else{
                                entity.setErrCode(actionResultBase.getErrorCode()+"");
                                entity.setErrMsg(actionResultBase.getMessage());
                                entity.setLastRequestId(null);
                                entity.setIsWaiting(0);
                                mapper.updateById(entity);
                            }
                        }
                    }
                    catch (Exception e){
                        entity.setErrCode("-10076");
                        entity.setErrMsg("返回出错："+e.getMessage()+"\n"+e.getStackTrace());
                        mapper.updateById(entity);
                    }

                }
            }
        }

        return true;
    }

    @Override
    public boolean uploadProductClass() {

        boolean bSuccess=false;
        Map<String,String> mapContrastFields=new HashMap<>();
        mapContrastFields.put("分类编号","calCode");
        mapContrastFields.put("分类名称","calName");
        mapContrastFields.put("标识码","eri");
        mapContrastFields.put("父项分类名称","pcode");
        //mapContrastFields.put("状态","");
        mapContrastFields.put("备注","calDesc");

        ProductClassUploadUtils UploadUtils =null;
        try{
            UploadUtils = new ProductClassUploadUtils(this.qqingflowApiInfo,
                    this.mysqlJdbcTemplate,this.qingAppfieldsMapper,
                    this.qingAppfieldtablevaluesMapper,
                    mapContrastFields,
                    "product_class");
        }
        catch (Exception e){
            log.error(e.getMessage()+"\n"+e.getStackTrace());
            return  false;
        }

        if(UploadUtils==null){
            return  false;
        }

        ProductClassEntity entity =null  ;

        ProductClassMapper mapper=productClassMapper;

        try {

            List<ProductClassEntity> listNoUpdate =null  ;

            //region 新增初始行
            QueryWrapper<ProductClassEntity> addQueryWrapper = new QueryWrapper<>();
            addQueryWrapper.last(" where (lastRequestId IS NULL or lastRequestId='') AND (isWaiting=0  or isWaiting is NULL ) AND (applyId IS NULL OR applyId=0)");

            listNoUpdate = mapper.selectList(addQueryWrapper);
            if (listNoUpdate == null || listNoUpdate.isEmpty()) {

            } else {
                //Action  shuold be "A"
                for (int i = 0; i < listNoUpdate.size(); i++) {
                    entity = listNoUpdate.get(i);
                    if (entity.getEri()==null||entity.getEri().isEmpty()){

                    }
                    else {


                        try {
                            QingAddDataResultMain resultMain = UploadUtils.AddEntity(entity);
                            //region 处理返回结果
                            if (resultMain == null) {

                                entity.setErrCode("-10015");
                                entity.setErrMsg("服务器返回内容为空！");
                                entity.setLastRequestId(null);
                                entity.setIsDeal(0);
                                entity.setIsWaiting(0);
                                entity.setDealDate(LocalDateTime.now());
                                mapper.updateById(entity);

                            } else {
                                if (resultMain.getErrCode() == 0) {
                                    entity.setErrCode(resultMain.getErrCode().toString());
                                    entity.setErrMsg("提交成功！");
                                    entity.setLastRequestId(resultMain.getResult().getRequestId());
                                    entity.setIsDeal(0);
                                    entity.setIsWaiting(1);
                                    entity.setDealDate(LocalDateTime.now());
                                    entity.setActionResult("A");
                                    mapper.updateById(entity);

                                } else {
                                    entity.setErrCode(resultMain.getErrCode().toString());
                                    entity.setErrMsg(resultMain.getErrMsg());
                                    entity.setLastRequestId(null);
                                    entity.setIsDeal(0);
                                    entity.setIsWaiting(0);
                                    entity.setDealDate(LocalDateTime.now());
                                    mapper.updateById(entity);
                                }

                            }
                            //endregion
                        } catch (Exception e) {
                            entity.setErrCode("-10076");
                            entity.setErrMsg(e.getMessage() + "\n" + e.getStackTrace());
                            entity.setLastRequestId(null);
                            entity.setIsDeal(0);
                            entity.setIsWaiting(0);
                            mapper.updateById(entity);
                        }
                    }
                }
            }
            //endregion



            //region 新增行
            addQueryWrapper = new QueryWrapper<>();
            addQueryWrapper.last(" where  (lastRequestId is null or lastRequestId='') and updAction='A' AND (isWaiting=0  or isWaiting is NULL )  and applyId>0");


            listNoUpdate = null;
            listNoUpdate = mapper.selectList(addQueryWrapper);
            if (listNoUpdate == null || listNoUpdate.isEmpty()) {

            } else {
                //Action  shuold be "A"
                for (int i = 0; i < listNoUpdate.size(); i++) {
                    entity = listNoUpdate.get(i);
                    try {
                        QingAddDataResultMain resultMain= UploadUtils.AddEntity(entity);
                    } catch (Exception e) {
                        entity.setErrCode("-10076");
                        entity.setErrMsg(e.getMessage() + "\n" + e.getStackTrace());
                        entity.setLastRequestId(null);
                        entity.setIsDeal(0);
                        entity.setIsWaiting(0);
                        mapper.updateById(entity);
                    }

                }
            }
            //endregion

            //修改

            //region 修改行
            addQueryWrapper = new QueryWrapper<>();
            addQueryWrapper.last(" where  (lastRequestId is null or lastRequestId='') and updAction='M' AND (isWaiting=0  or isWaiting is NULL )  and applyId>0");


            listNoUpdate = null;
            listNoUpdate = mapper.selectList(addQueryWrapper);
            if (listNoUpdate == null || listNoUpdate.isEmpty()) {

            } else {
                //Action  shuold be "A"
                for (int i = 0; i < listNoUpdate.size(); i++) {
                    entity = listNoUpdate.get(i);
                    try {
                        QingAddDataResultMain resultMain=  UploadUtils.UpdEntity(entity,"A");
                        //region 处理返回结果
                        if (resultMain == null) {

                            entity.setErrCode("-10015");
                            entity.setErrMsg("服务器返回内容为空！");
                            entity.setLastRequestId(null);
                            entity.setIsDeal(0);
                            entity.setIsWaiting(0);
                            entity.setDealDate(LocalDateTime.now());
                            mapper.updateById(entity);

                        } else {
                            if (resultMain.getErrCode() == 0) {
                                entity.setErrCode(resultMain.getErrCode().toString());
                                entity.setErrMsg("提交成功！");
                                entity.setLastRequestId(resultMain.getResult().getRequestId());
                                entity.setIsDeal(0);
                                entity.setIsWaiting(1);
                                entity.setDealDate(LocalDateTime.now());
                                entity.setActionResult("A");
                                mapper.updateById(entity);

                            } else {
                                entity.setErrCode(resultMain.getErrCode().toString());
                                entity.setErrMsg(resultMain.getErrMsg());
                                entity.setLastRequestId(null);
                                entity.setIsDeal(0);
                                entity.setIsWaiting(0);
                                entity.setDealDate(LocalDateTime.now());
                                mapper.updateById(entity);
                            }

                        }
                        //endregion
                    } catch (Exception e) {
                        entity.setErrCode("-10076");
                        entity.setErrMsg(e.getMessage() + "\n" + e.getStackTrace());
                        entity.setLastRequestId(null);
                        entity.setIsDeal(0);
                        entity.setIsWaiting(0);
                        mapper.updateById(entity);
                    }

                }
            }
            //endregion

            //删除条件

            //region 删除行
            addQueryWrapper = new QueryWrapper<>();
            addQueryWrapper.last(" where  (lastRequestId is null or lastRequestId='') and updAction='D' AND (isWaiting=0  or isWaiting is NULL )  and applyId>0");


            listNoUpdate = null;
            listNoUpdate = mapper.selectList(addQueryWrapper);
            if (listNoUpdate == null || listNoUpdate.isEmpty()) {

            } else {
                //Action  shuold be "D"
                for (int i = 0; i < listNoUpdate.size(); i++) {
                    entity = listNoUpdate.get(i);
                    //判断有没有使用，如果使用，只能修改，否则调删除
                    String strSql="SELECT COUNT(*)\n" +
                            " from product_class\n" +
                            " WHERE pcode=? or upper_class=?";
                    int iHaveCount=mysqlJdbcTemplate.queryForObject(strSql,new Object[]{entity.getCalCode()},Integer.class);

                    strSql="SELECT COUNT(*)\n" +
                            " from product_info\n" +
                            " WHERE invClass=?";
                    iHaveCount+=mysqlJdbcTemplate.queryForObject(strSql,new Object[]{entity.getCalCode()},Integer.class);

                    if(iHaveCount>0){
                        try {
                            QingAddDataResultMain resultMain= UploadUtils.UpdEntity(entity,"R");
                            //region 处理返回结果
                            if (resultMain == null) {

                                entity.setErrCode("-10015");
                                entity.setErrMsg("服务器返回内容为空！");
                                entity.setLastRequestId(null);
                                entity.setIsDeal(0);
                                entity.setIsWaiting(0);
                                entity.setDealDate(LocalDateTime.now());
                                mapper.updateById(entity);

                            } else {
                                if (resultMain.getErrCode() == 0) {
                                    entity.setErrCode(resultMain.getErrCode().toString());
                                    entity.setErrMsg("提交成功！");
                                    entity.setLastRequestId(resultMain.getResult().getRequestId());
                                    entity.setIsDeal(0);
                                    entity.setIsWaiting(1);
                                    entity.setDealDate(LocalDateTime.now());
                                    entity.setActionResult("A");
                                    mapper.updateById(entity);

                                } else {
                                    entity.setErrCode(resultMain.getErrCode().toString());
                                    entity.setErrMsg(resultMain.getErrMsg());
                                    entity.setLastRequestId(null);
                                    entity.setIsDeal(0);
                                    entity.setIsWaiting(0);
                                    entity.setDealDate(LocalDateTime.now());
                                    mapper.updateById(entity);
                                }

                            }
                            //endregion
                        } catch (Exception e) {
                            entity.setErrCode("-10076");
                            entity.setErrMsg(e.getMessage() + "\n" + e.getStackTrace());
                            entity.setLastRequestId(null);
                            entity.setIsDeal(0);
                            entity.setIsWaiting(0);
                            mapper.updateById(entity);
                        }

                    }
                    else{
                        try {
                            QingAddDataResultMain resultMain = UploadUtils.DelEntity(entity);

                            //region 处理返回结果

                            if (resultMain == null) {

                                entity.setErrCode("-10015");
                                entity.setErrMsg("服务器返回内容为空！");
                                entity.setLastRequestId(null);
                                entity.setIsDeal(0);
                                entity.setIsWaiting(0);
                                entity.setDealDate(LocalDateTime.now());
                                mapper.updateById(entity);

                            } else {
                                if (resultMain.getErrCode() == 0) {
                                    entity.setErrCode(resultMain.getErrCode().toString());
                                    entity.setErrMsg("提交成功！");
                                    entity.setLastRequestId(resultMain.getResult().getRequestId());
                                    entity.setIsDeal(0);
                                    entity.setIsWaiting(1);
                                    entity.setDealDate(LocalDateTime.now());
                                    entity.setActionResult("D");
                                    mapper.updateById(entity);
                                    //增加马上判断的结果

                                } else {
                                    entity.setErrCode(resultMain.getErrCode().toString());
                                    entity.setErrMsg(resultMain.getErrMsg());
                                    entity.setLastRequestId(null);
                                    entity.setIsDeal(0);
                                    entity.setIsWaiting(0);
                                    entity.setDealDate(LocalDateTime.now());
                                    mapper.updateById(entity);
                                }

                            }
                            //endregion

                        } catch (Exception e) {
                            entity.setErrCode("-10076");
                            entity.setErrMsg(e.getMessage() + "\n" + e.getStackTrace());
                            entity.setLastRequestId(null);
                            entity.setIsDeal(0);
                            entity.setIsWaiting(0);
                            mapper.updateById(entity);
                        }
                    }

                }
            }
            //endregion


        }
        catch (Exception e){
            log.error(e.getMessage()+"\n"+e.getStackTrace());
        }



        return true;
    }

    /**
     * 将运行的结果查询下来，返回正确的结果，或者 错误
     * @return
     */
    @Override
    public boolean QueryProductClassActionResult() {

        QueryWrapper<ProductClassEntity> queryWrapper=new QueryWrapper<>();
        ProductClassEntity entity =null  ;

        ProductClassMapper mapper=productClassMapper;
        queryWrapper=null;
        queryWrapper=new QueryWrapper<>();

        //queryWrapper.eq("isDeal",0)
        //        .and(wq->wq.eq("lastRequestId","")
        //                .or().isNull("lastRequestId")
        //        );
        queryWrapper.last(" where isDeal=0 and isWaiting=1 and NOT (lastRequestId is NULL or lastRequestId='')");

        List<ProductClassEntity> listQuery=mapper.selectList(queryWrapper);
        if (listQuery==null||listQuery.isEmpty()){

        }
        else {
            for (int i = 0; i < listQuery.size(); i++) {
                if(listQuery.get(i).getLastRequestId()==null||listQuery.get(i).getLastRequestId().isEmpty()){

                }
                else {
                    entity=listQuery.get(i);
                    try{
                        QingQueryActionResultBase actionResultBase= QingflowAPIUtils.GetRequestResult(qqingflowApiInfo,listQuery.get(i).getLastRequestId());
                        if (actionResultBase==null){
                            entity.setErrCode("-10066");
                            entity.setErrMsg("返回出错：返回内容为空！");
                            mapper.updateById(entity);
                        }
                        else{
                            if (actionResultBase.getErrorCode()==0){
                                if (entity.getActionResult().equals("A")){
                                    entity.setErrCode("0");
                                    entity.setErrMsg("更新成功！");
                                    entity.setLastRequestId(null);
                                    entity.setIsWaiting(0);
                                    entity.setIsNeedUpd(0);
                                    //entity.setUpdAction("");//清除
                                    entity.setIsDeal(1);
                                    try {
                                        long applyId= Long.parseLong(actionResultBase.getResult());
                                        entity.setApplyId(applyId);
                                    }
                                    catch (Exception e){
                                        e.printStackTrace();
                                    }

                                }
                                else if (entity.getActionResult().equals("D")){

                                    try {
                                        mapper.deleteById(entity);
                                    }
                                    catch (Exception e){
                                        e.printStackTrace();
                                    }

                                }

                                mapper.updateById(entity);
                            }
                            else{
                                entity.setErrCode(actionResultBase.getErrorCode()+"");
                                entity.setErrMsg(actionResultBase.getMessage());
                                entity.setLastRequestId(null);
                                entity.setIsWaiting(0);
                                mapper.updateById(entity);
                            }
                        }
                    }
                    catch (Exception e){
                        entity.setErrCode("-10076");
                        entity.setErrMsg("返回出错："+e.getMessage()+"\n"+e.getStackTrace());
                        mapper.updateById(entity);
                    }

                }
            }
        }

        return true;
    }

    @Override
    public boolean uploadproductInfo() {


        String strTableName="product_info";
        boolean bSuccess=false;
        Map<String,String> mapContrastFields=new HashMap<>();

        //mapContrastFields.put("分类编号","calCode");
        //mapContrastFields.put("分类名称","calName");
        //mapContrastFields.put("标识码","eri");
        //mapContrastFields.put("父项分类名称","pcode");
        //mapContrastFields.put("备注","calDesc");

        //208482466	产品编号	2			02-51001-128-2  invCode
        mapContrastFields.put("产品编号","invCode");
        //208482467	产品名称	2			限位臂 invName
        mapContrastFields.put("产品名称","invName");
        //208482468	规格型号	2			E38后门   invStd
        mapContrastFields.put("规格型号","invStd");
        //208482473	产品分类	19			限位臂 invClass
        mapContrastFields.put("产品分类","invClass");
        //208482480	计量单位	19			件   unitCode
        mapContrastFields.put("计量单位","unitCode");
        //208482492	状态	11			可用
        //208482488	产品备注	2			bz  invMemo
        mapContrastFields.put("产品备注","invMemo");
        //208485504	标识码	2			afasdfee    eri
        mapContrastFields.put("标识码","eri");

        //工艺路线（默认）
        mapContrastFields.put("工艺路线（默认）","defProuting");
        //客户代号
        mapContrastFields.put("客户代号","cusCode");
        //客户名称
        mapContrastFields.put("客户名称","cusName");

        //零件分类
        mapContrastFields.put("零件分类","partClass");

        //208482478	单重/g	8			12
        //208482491	模具名称	19			test-模具名称002
        //208482490	模具编号（默认）	19			test-模具编号002
        //208482484	工艺路线（默认）	19			标准工艺路线-test
        //208484951	色号	2			ads
        //208482487	单箱数量/件	8			90


        ProductInfoUploadUtils UploadUtils =null;
        try{
            UploadUtils = new ProductInfoUploadUtils(this.qqingflowApiInfo,
                    this.mysqlJdbcTemplate,
                    this.qingflowdbJdbcTemplate,
                    this.qingAppfieldsMapper,
                    this.qingAppfieldtablevaluesMapper,
                    mapContrastFields,
                    strTableName,"产品编号");
        }
        catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage()+"\n"+e.getStackTrace());
            return  false;
        }

        if(UploadUtils==null){
            return  false;
        }

        ProductInfoEntity entity =null  ;

        ProductInfoMapper mapper=productInfoMapper;

        try {

            List<ProductInfoEntity> listNoUpdate =null  ;

            //region 新增初始行
            QueryWrapper<ProductInfoEntity> addQueryWrapper = new QueryWrapper<>();
            addQueryWrapper.last(" where (lastRequestId IS NULL or lastRequestId='')" +
                    "  AND (isWaiting=0  or isWaiting is NULL ) " +
                    "  AND (applyId IS NULL OR applyId=0)" +
                    "   and invClass LIKE 'ZS%' ");//

            listNoUpdate = mapper.selectList(addQueryWrapper);
            if (listNoUpdate == null || listNoUpdate.isEmpty()) {

            } else {
                //Action  shuold be "A"
                for (int i = 0; i < listNoUpdate.size(); i++) {
                    entity = listNoUpdate.get(i);
                    if (entity.getEri()==null||entity.getEri().isEmpty()){

                    }
                    else {

                        //判断一下，是不是已经手工建好了。
                        String strcInvCode=entity.getInvCode();

                        Long lCheckApplyId= UploadUtils.checkEntityExists(strcInvCode);
                        if(lCheckApplyId>0){

                            //如果已经存在，则需要将其改成更新状态
                            entity.setApplyId(lCheckApplyId);
                            entity.setUpdAction("M");
                            entity.setIsDeal(0);
                            entity.setIsNeedUpd(1);
                            entity.setLastRequestId(lCheckApplyId+"");
                            mapper.updateById(entity);

                        }
                        else {

                            if (entity.getDefProuting() == null || entity.getDefProuting().isEmpty()) {

                                String strSql = "SELECT getInvDefRouting('" + entity.getInvCode() + "');";
                                String strDefRouting = mysqlJdbcTemplate.queryForObject(strSql, String.class);
                                entity.setDefProuting(strDefRouting);
                            }
                            if (entity.getPartClass() == null || entity.getPartClass().isEmpty()) {

                                String strSql = "SELECT getInvPartClass('" + entity.getInvCode() + "');";
                                String strDefRouting = mysqlJdbcTemplate.queryForObject(strSql, String.class);
                                entity.setPartClass(strDefRouting);
                            }
                            try {
                                QingAddDataResultMain resultMain = UploadUtils.AddEntity(entity);
                                //region 处理返回结果
                                if (resultMain == null) {

                                    entity.setErrCode("-10015");
                                    entity.setErrMsg("服务器返回内容为空！");
                                    entity.setLastRequestId(null);
                                    entity.setIsDeal(0);
                                    entity.setIsWaiting(0);
                                    entity.setDealDate(LocalDateTime.now());
                                    mapper.updateById(entity);

                                } else {
                                    if (resultMain.getErrCode() == 0) {
                                        entity.setErrCode(resultMain.getErrCode().toString());
                                        entity.setErrMsg("提交成功！");
                                        entity.setLastRequestId(resultMain.getResult().getRequestId());
                                        entity.setIsDeal(0);
                                        entity.setIsWaiting(1);
                                        entity.setDealDate(LocalDateTime.now());
                                        entity.setActionResult("A");
                                        mapper.updateById(entity);

                                    } else {
                                        entity.setErrCode(resultMain.getErrCode().toString());
                                        entity.setErrMsg(resultMain.getErrMsg());
                                        entity.setLastRequestId(null);
                                        entity.setIsDeal(0);
                                        entity.setIsWaiting(0);
                                        entity.setDealDate(LocalDateTime.now());
                                        mapper.updateById(entity);
                                    }

                                }
                                //endregion
                            } catch (Exception e) {
                                entity.setErrCode("-10076");
                                entity.setErrMsg(e.getMessage() + "\n" + e.getStackTrace());
                                entity.setLastRequestId(null);
                                entity.setIsDeal(0);
                                entity.setIsWaiting(0);
                                mapper.updateById(entity);
                            }
                        }
                    }
                }
            }
            //endregion



            //region 新增行
            addQueryWrapper = new QueryWrapper<>();
            addQueryWrapper.last(" where  (lastRequestId is null or lastRequestId='') and updAction='A'" +
                    " AND (isWaiting=0  or isWaiting is NULL ) " +
                    " and applyId>0 and invClass LIKE 'ZS%' ");


            listNoUpdate = null;
            listNoUpdate = mapper.selectList(addQueryWrapper);
            if (listNoUpdate == null || listNoUpdate.isEmpty()) {

            } else {
                //Action  shuold be "A"
                for (int i = 0; i < listNoUpdate.size(); i++) {
                    entity = listNoUpdate.get(i);

                    if(entity.getDefProuting()==null||entity.getDefProuting().isEmpty()){

                        String strSql="SELECT getInvDefRouting('"+entity.getInvCode()+"');";
                        String strDefRouting=mysqlJdbcTemplate.queryForObject(strSql,String.class);
                        entity.setDefProuting(strDefRouting);
                    }
                    if(entity.getPartClass()==null||entity.getPartClass().isEmpty()){

                        String strSql="SELECT getInvPartClass('"+entity.getInvCode()+"');";
                        String strDefRouting=mysqlJdbcTemplate.queryForObject(strSql,String.class);
                        entity.setPartClass(strDefRouting);
                    }
                    //判断一下，是不是已经手工建好了。
                    Long lCheckApplyId= UploadUtils.checkEntityExists(entity.getInvCode());
                    if(lCheckApplyId>0){

                        //如果已经存在，则需要将其改成更新状态
                        entity.setApplyId(lCheckApplyId);
                        entity.setUpdAction("M");
                        entity.setIsDeal(0);
                        entity.setIsNeedUpd(1);
                        entity.setLastRequestId(lCheckApplyId+"");
                        mapper.updateById(entity);

                    }
                    else {
                        try {
                            QingAddDataResultMain resultMain = UploadUtils.AddEntity(entity);
                            //region 处理返回结果
                            if (resultMain == null) {

                                entity.setErrCode("-10015");
                                entity.setErrMsg("服务器返回内容为空！");
                                entity.setLastRequestId(null);
                                entity.setIsDeal(0);
                                entity.setIsWaiting(0);
                                entity.setDealDate(LocalDateTime.now());
                                mapper.updateById(entity);

                            } else {
                                if (resultMain.getErrCode() == 0) {
                                    entity.setErrCode(resultMain.getErrCode().toString());
                                    entity.setErrMsg("提交成功！");
                                    entity.setLastRequestId(resultMain.getResult().getRequestId());
                                    entity.setIsDeal(0);
                                    entity.setIsWaiting(1);
                                    entity.setDealDate(LocalDateTime.now());
                                    entity.setActionResult("A");
                                    mapper.updateById(entity);

                                } else {
                                    entity.setErrCode(resultMain.getErrCode().toString());
                                    entity.setErrMsg(resultMain.getErrMsg());
                                    entity.setLastRequestId(null);
                                    entity.setIsDeal(0);
                                    entity.setIsWaiting(0);
                                    entity.setDealDate(LocalDateTime.now());
                                    mapper.updateById(entity);
                                }

                            }
                            //endregion
                        } catch (Exception e) {
                            entity.setErrCode("-10076");
                            entity.setErrMsg(e.getMessage() + "\n" + e.getStackTrace());
                            entity.setLastRequestId(null);
                            entity.setIsDeal(0);
                            entity.setIsWaiting(0);
                            mapper.updateById(entity);
                        }
                    }
                }
            }
            //endregion

            //region 修改行
            addQueryWrapper = new QueryWrapper<>();
            addQueryWrapper.last(" where  (lastRequestId is null or lastRequestId='') " +
                    " and updAction='M' " +
                    " AND (isWaiting=0  or isWaiting is NULL )  " +
                    " and applyId>0" +
                    "  ");//and invClass LIKE 'ZS%'

            listNoUpdate = null;
            listNoUpdate = mapper.selectList(addQueryWrapper);
            if (listNoUpdate == null || listNoUpdate.isEmpty()) {

            } else {
                //Action  shuold be "A"
                for (int i = 0; i < listNoUpdate.size(); i++) {
                    entity = listNoUpdate.get(i);
                    if(entity.getDefProuting()==null||entity.getDefProuting().isEmpty()){

                        String strSql="SELECT getInvDefRouting('"+entity.getInvCode()+"');";
                        String strDefRouting=mysqlJdbcTemplate.queryForObject(strSql,String.class);
                        entity.setDefProuting(strDefRouting);
                    }
                    if(entity.getPartClass()==null||entity.getPartClass().isEmpty()){

                        String strSql="SELECT getInvPartClass('"+entity.getInvCode()+"');";
                        String strDefRouting=mysqlJdbcTemplate.queryForObject(strSql,String.class);
                        entity.setPartClass(strDefRouting);
                    }
                    //判断一下，是不是已经手工建好了。
                    Long lCheckApplyId= UploadUtils.checkEntityExists(entity.getInvCode());
                    if(lCheckApplyId>0){

                        Long lLocalApplyId=entity.getApplyId();
                        if(lCheckApplyId.equals(lLocalApplyId)){

                            try {

                                //判断一下，是不是已经删除了？

                                QingAddDataResultMain resultMain=  UploadUtils.UpdEntity(entity,"A");
                                //region 处理返回结果
                                if (resultMain == null) {

                                    entity.setErrCode("-10015");
                                    entity.setErrMsg("服务器返回内容为空！");
                                    entity.setLastRequestId(null);
                                    entity.setIsDeal(0);
                                    entity.setIsWaiting(0);
                                    entity.setDealDate(LocalDateTime.now());
                                    mapper.updateById(entity);

                                } else {
                                    if (resultMain.getErrCode() == 0) {
                                        entity.setErrCode(resultMain.getErrCode().toString());
                                        entity.setErrMsg("提交成功！");
                                        entity.setLastRequestId(resultMain.getResult().getRequestId());
                                        entity.setIsDeal(0);
                                        entity.setIsWaiting(1);
                                        entity.setDealDate(LocalDateTime.now());
                                        entity.setActionResult("A");
                                        mapper.updateById(entity);

                                    } else {
                                        entity.setErrCode(resultMain.getErrCode().toString());
                                        entity.setErrMsg(resultMain.getErrMsg());
                                        entity.setLastRequestId(null);
                                        entity.setIsDeal(0);
                                        entity.setIsWaiting(0);
                                        entity.setDealDate(LocalDateTime.now());
                                        mapper.updateById(entity);
                                    }

                                }
                                //endregion
                            } catch (Exception e) {
                                entity.setErrCode("-10076");
                                entity.setErrMsg(e.getMessage() + "\n" + e.getStackTrace());
                                entity.setLastRequestId(null);
                                entity.setIsDeal(0);
                                entity.setIsWaiting(0);
                                mapper.updateById(entity);
                            }
                        }
                        else{

                            //表示已经删除，手工加过了 。
                            entity.setApplyId(0l);
                            entity.setIsWaiting(0);
                            entity.setIsNeedUpd(1);
                            entity.setIsDeal(0);
                            entity.setUpdAction("A");
                            entity.setLastRequestId(null);
                            mapper.updateById(entity);

                        }
                    }
                    else{
                        //如果不存在，则表示要新增
                        entity.setApplyId(0l);
                        entity.setIsWaiting(0);
                        entity.setIsNeedUpd(1);
                        entity.setIsDeal(0);
                        entity.setUpdAction("A");
                        entity.setLastRequestId(null);
                        mapper.updateById(entity);
                    }

                }
            }
            //endregion

            //删除条件

            //region 删除行
            addQueryWrapper = new QueryWrapper<>();
            addQueryWrapper.last(" where  (lastRequestId is null or lastRequestId='') " +
                    "and updAction='D' AND (isWaiting=0  or isWaiting is NULL ) " +
                    " and applyId>0" +
                    "  ");//and invClass LIKE 'ZS%'


            listNoUpdate = null;
            listNoUpdate = mapper.selectList(addQueryWrapper);
            if (listNoUpdate == null || listNoUpdate.isEmpty()) {

            } else {
                //Action  shuold be "D"
                for (int i = 0; i < listNoUpdate.size(); i++) {
                    entity = listNoUpdate.get(i);



                    //判断有没有使用，如果使用，只能修改，否则调删除
                    String strSql="SELECT COUNT(*)\n" +
                            " from bomwork \n" +
                            " WHERE MPROD=? ";
                    int iHaveCount=mysqlJdbcTemplate.queryForObject(strSql,new Object[]{entity.getInvCode()},Integer.class);

                    if(iHaveCount>0){
                        try {
                            QingAddDataResultMain resultMain= UploadUtils.UpdEntity(entity,"R");
                            //region 处理返回结果
                            if (resultMain == null) {

                                entity.setErrCode("-10015");
                                entity.setErrMsg("服务器返回内容为空！");
                                entity.setLastRequestId(null);
                                entity.setIsDeal(0);
                                entity.setIsWaiting(0);
                                entity.setDealDate(LocalDateTime.now());
                                mapper.updateById(entity);

                            } else {
                                if (resultMain.getErrCode() == 0) {
                                    entity.setErrCode(resultMain.getErrCode().toString());
                                    entity.setErrMsg("提交成功！");
                                    entity.setLastRequestId(resultMain.getResult().getRequestId());
                                    entity.setIsDeal(0);
                                    entity.setIsWaiting(1);
                                    entity.setDealDate(LocalDateTime.now());
                                    entity.setActionResult("A");
                                    mapper.updateById(entity);

                                } else {
                                    entity.setErrCode(resultMain.getErrCode().toString());
                                    entity.setErrMsg(resultMain.getErrMsg());
                                    entity.setLastRequestId(null);
                                    entity.setIsDeal(0);
                                    entity.setIsWaiting(0);
                                    entity.setDealDate(LocalDateTime.now());
                                    mapper.updateById(entity);
                                }

                            }
                            //endregion
                        } catch (Exception e) {
                            entity.setErrCode("-10076");
                            entity.setErrMsg(e.getMessage() + "\n" + e.getStackTrace());
                            entity.setLastRequestId(null);
                            entity.setIsDeal(0);
                            entity.setIsWaiting(0);
                            mapper.updateById(entity);
                        }

                    }
                    else{
                        try {
                            QingAddDataResultMain resultMain = UploadUtils.DelEntity(entity);

                            //region 处理返回结果

                            if (resultMain == null) {

                                entity.setErrCode("-10015");
                                entity.setErrMsg("服务器返回内容为空！");
                                entity.setLastRequestId(null);
                                entity.setIsDeal(0);
                                entity.setIsWaiting(0);
                                entity.setDealDate(LocalDateTime.now());
                                mapper.updateById(entity);

                            } else {
                                if (resultMain.getErrCode() == 0) {
                                    entity.setErrCode(resultMain.getErrCode().toString());
                                    entity.setErrMsg("提交成功！");
                                    entity.setLastRequestId(resultMain.getResult().getRequestId());
                                    entity.setIsDeal(0);
                                    entity.setIsWaiting(1);
                                    entity.setDealDate(LocalDateTime.now());
                                    entity.setActionResult("D");
                                    mapper.updateById(entity);
                                    //增加马上判断的结果

                                } else {
                                    entity.setErrCode(resultMain.getErrCode().toString());
                                    entity.setErrMsg(resultMain.getErrMsg());
                                    entity.setLastRequestId(null);
                                    entity.setIsDeal(0);
                                    entity.setIsWaiting(0);
                                    entity.setDealDate(LocalDateTime.now());
                                    mapper.updateById(entity);
                                }

                            }
                            //endregion

                        } catch (Exception e) {
                            entity.setErrCode("-10076");
                            entity.setErrMsg(e.getMessage() + "\n" + e.getStackTrace());
                            entity.setLastRequestId(null);
                            entity.setIsDeal(0);
                            entity.setIsWaiting(0);
                            mapper.updateById(entity);
                        }
                    }

                }
            }
            //endregion


        }
        catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage()+"\n"+e.getStackTrace());
        }



        return true;
    }

    @Override
    public boolean QueryProductInfoActionResult() {

        QueryWrapper<ProductInfoEntity> queryWrapper=new QueryWrapper<>();
        ProductInfoEntity entity =null  ;

        ProductInfoMapper mapper=productInfoMapper;
        queryWrapper=null;
        queryWrapper=new QueryWrapper<>();

        //queryWrapper.eq("isDeal",0)
        //        .and(wq->wq.eq("lastRequestId","")
        //                .or().isNull("lastRequestId")
        //        );
        queryWrapper.last(" where isDeal=0 and isWaiting=1 and NOT (lastRequestId is NULL or lastRequestId='')");

        List<ProductInfoEntity> listQuery=mapper.selectList(queryWrapper);
        if (listQuery==null||listQuery.isEmpty()){

        }
        else {
            for (int i = 0; i < listQuery.size(); i++) {
                if(listQuery.get(i).getLastRequestId()==null||listQuery.get(i).getLastRequestId().isEmpty()){

                }
                else {
                    entity=listQuery.get(i);
                    try{
                        QingQueryActionResultBase actionResultBase= QingflowAPIUtils.GetRequestResult(qqingflowApiInfo,listQuery.get(i).getLastRequestId());
                        if (actionResultBase==null){
                            entity.setErrCode("-10066");
                            entity.setErrMsg("返回出错：返回内容为空！");
                            mapper.updateById(entity);
                        }
                        else{
                            if (actionResultBase.getErrorCode()==0){
                                if (entity.getActionResult().equals("A")){
                                    entity.setErrCode("0");
                                    entity.setErrMsg("更新成功！");
                                    entity.setLastRequestId(null);
                                    entity.setIsWaiting(0);
                                    entity.setIsNeedUpd(0);
                                    //entity.setUpdAction("");//清除
                                    entity.setIsDeal(1);
                                    try {
                                        long applyId= Long.parseLong(actionResultBase.getResult());
                                        entity.setApplyId(applyId);
                                    }
                                    catch (Exception e){
                                        e.printStackTrace();
                                    }

                                }
                                else if (entity.getActionResult().equals("D")){

                                    try {
                                        mapper.deleteById(entity);
                                    }
                                    catch (Exception e){
                                        e.printStackTrace();
                                    }

                                }

                                mapper.updateById(entity);
                            }
                            else{
                                entity.setErrCode(actionResultBase.getErrorCode()+"");
                                entity.setErrMsg(actionResultBase.getMessage());
                                entity.setLastRequestId(null);
                                entity.setIsWaiting(0);
                                mapper.updateById(entity);
                            }
                        }
                    }
                    catch (Exception e){
                        entity.setErrCode("-10076");
                        entity.setErrMsg("返回出错："+e.getMessage()+"\n"+e.getStackTrace());
                        mapper.updateById(entity);
                    }

                }
            }
        }

        return true;
    }




}
